var webName = "/tbpoject";
var webFore = webName + "/foreground";
var loginPath = webFore + "/login.html";//登录
var indexPath = webFore + "/index.html";//首页
var registerPath = webFore + "/register.html";//注册页面
var accManage = webFore + "/zhanghao.html";//我的淘宝
var shopPath = webFore + "/myshoppingcar.html";//购物车
var goodsCollection = webFore + "/collection.html";//收藏宝贝
var storeCollection = webFore + "/storecollection.html";//收藏店铺

$(document).ready(function(){
	accountAJAX();
	searchAJAX();
	hotspotAJAX();
	imageAJXA();

	
});

//账户异步请求
function accountAJAX(){
	$.ajax({
        type : 'post',
        url  : '/tbpoject/customerControler/getCustomerJson',
        contentType : 'application/json; charset = utf-8',
        data : '',
        dataType : 'json',
        success : function(data){
            if(data != null){
            	//导航栏展示
            	$("#arrow1 > a").attr("href", accManage);
            	$("#arrow3 > a").attr("href", accManage);
            	$("#arrow4 > a").attr("href", shopPath);
            	$("#arrow5 > a").attr("href", goodsCollection);
            		$("#collectionRen").bind("click",function(){
            			location.href=goodsCollection;
            		})
            		$("#collectionStore").bind("click",function(){
            			location.href=storeCollection;
            		})
            	
            	//用户信息展示
                $(".inner-user > h3").text("Hi " + data.name);
            	$("#loginBox1 > a").remove();
            	$("#loginBox1").html("<a class='login-btn free' href='" + accManage + "'>我的淘宝</a>&nbsp; " +
            			"<a class='login-btn free' href='/tbpoject/customerControler/quitCustomer'>退出</a>");
            }else{
            	//导航栏展示
            	$("#arrow1").html("<a href = '"+loginPath+"' >请先登录</a>");
            	$("#nav-box1").remove();	
            	$("#arrow2").html("<a href = '"+registerPath+"' >免费注册</a>");
            	$("#nav-box2").remove();
            	$("#arrow3").remove();
            	$("#nav-box3").remove();
            	$("#arrow4").remove();
            	$("#nav-box4").remove();
            	$("#arrow5").remove();
            	$("#nav-box5").remove();
            	
            	//个人信息展示
                $(".inner-user > h3").text("Hi 你好!");
            }
        },
        error : function(){
            alert("请检查网络或联系管理员")
        }
    });
}



//搜索异步请求
function searchAJAX(){
	$(".search-but").click(function(){
		$.post(
				'/tbpoject/goodsControler/setSearch',
				{"searchValue" : $(".search-in").val() },
				function(data,status){
					window.location="/tbpoject/foreground/searchGoods.html";
				}
	)});	
}

//热点异步布局
function hotspotAJAX(){
	$.ajax({
        type : 'post',
        url  : '/tbpoject/hotSpotlgwControler/getAllHotSpot',
        contentType : 'application/json; charset = utf-8',
        data : '',
        dataType : 'json',
        success : function(data){
        	var txt = '';
        	var txt1 = '';
        	var txt2 = '';
            for(var i = 0; i < data.length; i++){
                txt1+= '<div class="sub-nav-cell" id = "hotSpot' + data[i].id + '">';
                txt2+= '<div class="sub-nav-cell right-shadow" id = "hotSpot' + data[i].id + '">';
                txt = '';
                txt += '<h3 class="green-sp">';
                txt += data[i].main;
                txt += '</h3>';
                txt += '<ul class="sub-nav-box" id = "hotSpotUl' + data[i].id + '">';
                for(var j=1; j < 17; j++){
                	txt += '<li><a href="#">';
                	txt += data[i]['message'+j];
                	txt += '</a></li>';
                }
                txt += '</ul></div>';
                txt1 = txt1 + txt;
                txt2 = txt2 + txt;
            }
            $("#hotSpotControler").append(txt1);
            $("#sub-nav100").append(txt2);
            
        },
        error : function(){
            alert("请检查网络或联系管理员")
        }
    });
}





function  loadImg(num){
    var $iBox = $('.imgBox');
    var $iNum = $('.imgNum');  //缓存优化
    var indexImg = 1;        //初始下标
    var totalImg = num;          //图片总数量
    var imgSize = 520;       //图片尺寸 宽度
    var moveTime = 1100;        //切换动画时间
    var setTime = 2500;       //中间暂停时间
    var clc = null;
    function moveImg(){
        if(indexImg != totalImg){
            $iBox.animate({
                left: -(indexImg*imgSize) + 'px'
            }, moveTime);
            $iNum.removeClass('mark-color')
                .eq(indexImg)
                .addClass('mark-color');
            indexImg++;
        }
        else{
            indexImg = 1;
            $iNum.removeClass('mark-color')
                .eq(indexImg - 1)
                .addClass('mark-color');
            $iBox.animate({
                left: 0
            }, moveTime);
        }
    }
    $iNum.hover(function(){
        $iBox.stop();                    //结束当前动画
        clearInterval(clc);              //暂停循环
        $iNum.removeClass('mark-color');
        $(this).addClass('mark-color');
        indexImg = $(this).index();
        $iBox.animate({
            left: -(indexImg*imgSize) + 'px'
        }, 500);
    },function(){
        clc = setInterval(moveImg, setTime);
    });

    //鼠标放上停止动画
    $iBox.hover(function(){
        $('.control').fadeIn(200);
        clearInterval(clc);              //暂停循环
    },function(){
        $('.control').hide();
        clc = setInterval(moveImg, setTime);
    });
    //显示左右
    $('.control').hover(function(){
        clearInterval(clc);
        $('.control').show();
//        return false;允许传播
    });
    //向右边前进
    $('.to-right').click(function(){
        if(indexImg != totalImg){
            $iBox.animate({
                left: -(indexImg*imgSize) + 'px'
            }, moveTime);
            $iNum.removeClass('mark-color')
                .eq(indexImg)
                .addClass('mark-color');
            indexImg++;
        }
        else{
            indexImg = 1;
            $iNum.removeClass('mark-color')
                .eq(indexImg - 1)
                .addClass('mark-color');
            $iBox.animate({
                left: 0
            }, moveTime);
        }
    });
    //向左边前进
    $('.to-left').click(function(){
        indexImg--;
        if(indexImg != 0){
            $iBox.animate({
                left: -((indexImg - 1)*imgSize) + 'px'
            }, moveTime);
            $iNum.removeClass('mark-color')
                .eq((indexImg - 1))
                .addClass('mark-color');
        }
        else{
            indexImg = totalImg;
            $iNum.removeClass('mark-color')
                .eq(indexImg - 1)
                .addClass('mark-color');
            $iBox.animate({
                left: -((indexImg - 1)*imgSize) + 'px'
            }, moveTime);
        }
    });
    clc = setInterval(moveImg, setTime);
}

//图片二
function loadImg1(num){
    var $iBox = $('.imgBox1');
    var $iNum = $('.imgNum1');  //缓存优化
    var indexImg = 1;        //初始下标
    var totalImg = num;          //图片总数量
    var imgSize = 520;       //图片尺寸 宽度
    var moveTime = 1100;        //切换动画时间
    var setTime = 2500;       //中间暂停时间
    var clc = null;
  function moveImg(){
      if(indexImg != totalImg){
          $iBox.animate({
              left: -(indexImg*imgSize) + 'px'
          }, moveTime);
          $iNum.removeClass('mark-color')
              .eq(indexImg)
              .addClass('mark-color');
          indexImg++;
      }
      else{
          indexImg = 1;
          $iNum.removeClass('mark-color')
              .eq(indexImg - 1)
              .addClass('mark-color');
          $iBox.animate({
              left: 0
          }, moveTime);
      }
  }
  $iNum.hover(function(){
      $iBox.stop();                    //结束当前动画
      clearInterval(clc);              //暂停循环
      $iNum.removeClass('mark-color');
      $(this).addClass('mark-color');
      indexImg = $(this).index();
      $iBox.animate({
          left: -(indexImg*imgSize) + 'px'
      }, 500);
  },function(){
      clc = setInterval(moveImg, setTime);
  });


  //鼠标放上停止动画
  $iBox.hover(function(){
      $('.control1').fadeIn(200);
      clearInterval(clc);              //暂停循环
  },function(){
      $('.control1').hide();
      clc = setInterval(moveImg, setTime);
  });
  //显示左右
  $('.control1').hover(function(){
      clearInterval(clc);
      $('.control1').show();
//      return false;允许传播
  });

  //向右边前进
  $('.to-right1').click(function(){
      if(indexImg != totalImg){
          $iBox.animate({
              left: -(indexImg*imgSize) + 'px'
          }, moveTime);
          $iNum.removeClass('mark-color')
              .eq(indexImg)
              .addClass('mark-color');
          indexImg++;
      }
      else{
          indexImg = 1;
          $iNum.removeClass('mark-color')
              .eq(indexImg - 1)
              .addClass('mark-color');
          $iBox.animate({
              left: 0
          }, moveTime);
      }
  });
  //向左边前进
  $('.to-left1').click(function(){
      indexImg--;
      if(indexImg != 0){
          $iBox.animate({
              left: -((indexImg - 1)*imgSize) + 'px'
          }, moveTime);
          $iNum.removeClass('mark-color')
              .eq((indexImg - 1))
              .addClass('mark-color');
      }
      else{
          indexImg = totalImg;
          $iNum.removeClass('mark-color')
              .eq(indexImg - 1)
              .addClass('mark-color');
          $iBox.animate({
              left: -((indexImg - 1)*imgSize) + 'px'
          }, moveTime);
      }
  });

  clc = setInterval(moveImg, setTime);
}
//轮转异步布局
function imageAJXA(){
	var num = 0;
	$.ajax({
        type : 'post',
        url  : '/tbpoject/picturelgwControler/getPictures',
        contentType : 'application/json; charset = utf-8',
        data : '',
        dataType : 'json',
        success : 
        	function(data){
        		var txt = '';
        		var txt1 = '';
        		var txt2 = '';
        		var txt3 = '';
        		for(var i = 0; i < data.length; i++){
        			if(data[i].isNav != '1') {
        				continue;
        			}
        			txt += '<li><a href = ""><img src="img/' + data[i].url + '"></a><li>';
        			txt3+= '<li><a href = ""><img src="img/' + data[i].url + '"></a><li>';
        			num += 1;

        		}
    			txt1+= '<span class = "imgNum mark-color"></span>';
    			txt2+= '<span class = "imgNum1 mark-color"></span>';
        		for(var i = 0; i < num-1; i++){
        			txt1+= '<span class = "imgNum"></span>';
        			txt2+= '<span class = "imgNum1"></span>';
        		}
        		$(".imgBox").html(txt);
        		$(".imgBox1").html(txt3);
        		$("#currentNum99").html(txt1);
        		$("#currentNum100").html(txt2);
        		$(".imgBox").width(num*520);
        		$(".imgBox1").width(num*520);
        		loadImg(num);
        		loadImg1(num);
        	},
        error : function(){
            alert("请检查网络或联系管理员")
        }
    });

}


